package com.surelution.cppei.card

import org.apache.commons.io.IOUtils;
import org.apache.http.Consts
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.NameValuePair
import org.apache.http.client.HttpClient
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.HttpPost
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.message.BasicNameValuePair
import org.apache.http.util.EntityUtils

import com.surelution.cppei.weixin.gate.GasCardType
import com.surelution.cppei.weixin.gate.SubscriberGascard

class CardService {
	
	String API_BASE_URL = "http://11.0.27.139:8080/petrosales-wechat-appserver-1.0/"

	/**
	 * 
	 * @param card
	 * @return
	 * 
	 *  * 提供卡号、证件号查询，成功返回：
	 * 
	<DATA>
		<RESULT>1</RESULT>
		<CONSSET>
			<singleItem name="gender">1</singleItem>
			<singleItem name="name">潘泉锦</singleItem>
			<singleItem name="idnoType">1</singleItem>
			<singleItem name="idno">361121199011212414</singleItem>
			<singleItem name="ownerId">19425014</singleItem>
			<singleItem name="cardAsn">9010000000003601</singleItem>
			<singleItem name="userType">1</singleItem>	    
		</CONSSET>			
	</DATA>
	 * 
	 * 失败则返回：
	 * 
	<DATA>
		<RESULT>0</RESULT>
		<ERROR>没有找到此用户</ERROR>			
	</DATA>
	 * 
	 */
    def validateCard(SubscriberGascard card) {
		def cardNo = card.cardNo
		def certificate = card.certificateNo
		
		String url = "${API_BASE_URL}getUserRegisterInfo.action";
		HttpPost post = new HttpPost(url);
		HttpClient client = new DefaultHttpClient();
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("key", "7/ZM+0PP9xTxMvQKPjpgYg=="));
		nvps.add(new BasicNameValuePair("cardAsn", cardNo));
		nvps.add(new BasicNameValuePair("idno", certificate));
		post.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
		HttpResponse response = client.execute(post);
		HttpEntity entity = response.getEntity();
		InputStream is = entity.getContent();
		
		def parser = new XmlParser().parse(is)
		
//		def lines = IOUtils.readLines(is)
//		String line = ""
//		lines.each {
//			line += it
//			line += "\n"
//		}
		
//		println line
//		def parser = new XmlParser().parseText(line)
		
		def result = parser.RESULT.text()
		println result
		if(result == "1") {
			def userTypeKey = parser.CONSSET.singleItem.find() {item->
				item.'@name'.contains('userType')
			}
			println userTypeKey.text()
			def userid = parser.CONSSET.singleItem.find() {item->
				item.'@name'.contains('ownerId')
			}
			println userid.text()
			def cardType = GasCardType.findByKey(userTypeKey.text())
			if(cardType) {
				card.cardType = cardType
				card.userid = userid.text().toString()
				card.certificated = true
				card.save(flush:true)
				return true
			}
		}
		EntityUtils.consume(entity);
		false
    }
	
	String fakeXml = """
	<DATA>
		<RESULT>1</RESULT>
		<CONSSET>
			<singleItem name="gender">1</singleItem>
			<singleItem name="name">潘泉锦</singleItem>
			<singleItem name="idnoType">1</singleItem>
			<singleItem name="idno">361121199011212414</singleItem>
			<singleItem name="ownerId">19425014</singleItem>
			<singleItem name="cardAsn">9010000000003601</singleItem>
			<singleItem name="userType">1</singleItem>	    
		</CONSSET>			
	</DATA>
"""
}
